/**
 * 
 */
package com.hs.core.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.hs.auth.AUTHConstant;
import com.hs.auth.GLOBAL_CONSTATNT;
import com.hs.auth.model.HsAccount;
import com.hs.core.cache.DictCacheUtil;
import com.hs.core.db.DBMgr;
import com.hs.core.model.BaseBean;

/**
 * @author shl
 *
 * Dec 9, 2010
 */
public abstract class BaseAction {
	/**
	 * 获取当前登录用户
	 * @param request
	 * @return
	 */
	protected HsAccount getCurrentuser(HttpServletRequest request){
		HsAccount accInfo = (HsAccount)this.getSession(request).getAttribute(AUTHConstant.USERINSESSION); 
	 	   return  accInfo; 
	}
	/**
	 * 获取当前session
	 * @param request
	 * @return
	 */
	protected HttpSession getSession(HttpServletRequest request){
		if(null == request)
			return null;
		return request.getSession();
	}
	/**
	 *初始化用户信息于session
	 * @param request
	 * @return
	 */
	protected void initSession(HttpServletRequest request,BaseBean obj){
		 HttpSession session = this.getSession(request);
		 if(null != session){
			 session.setAttribute(AUTHConstant.USERINSESSION, obj);
			 System.out.println("initSession:" + session.getAttribute(AUTHConstant.USERINSESSION));
		 }
			 
	}
	/**
	 * 加载用户权限
	 * @param request
	 * @param accid
	 */
	protected void loadAuth(HttpServletRequest request,String accid){
		 DBMgr dbo = this.getDbo(request);
		 List roleList = dbo.queryList("select ROLECODE from HS_ROLE WHERE ID IN (SELECT ROLEID FROM HS_ACCROLE where accid=?)", new Object[]{accid});
		 Map authmap = new HashMap();
		 for(int i =0;i<roleList.size();i++){
			 Map data = (Map)roleList.get(i);
			 String roleCode = (String)data.get("ROLECODE");  
			 authmap.put(roleCode, roleCode);
		 }
		 
		 HttpSession session = this.getSession(request);
		 System.out.println("session-----:" +authmap);
		 if(null != session){
			
			  session.setAttribute("USERPOWER_"+accid, authmap);
		 }
			 
	}
	 /**
	    * 根据用户id获取当前用户权限
	    * @param request
	    * @param userId
	    * @return
	    */
	   protected Map getUserRoleByUserid(HttpServletRequest request,Long userId){
		   HttpSession session =  this.getHttpSession(request);
		   
		   Map powerMap = null;
		   if(null != session){
			   powerMap = (Map)session.getAttribute("USERPOWER_" + userId);
			   System.out.println("userpower:" +powerMap );
		   }
		   return powerMap;
	   }
	   /**
	    * 获取session
	    * @param request
	    * @return
	    */
	   protected HttpSession getHttpSession(HttpServletRequest request){
		   if(null != request)
			 return   request.getSession();
		   return null;
	   }
	   /**
	    * 根据用户id获取当前用户角色
	    * @param request
	    * @param userId
	    * @return
	    */
	   protected Map getUserRoleCodeByUserid(HttpServletRequest request,Long userId){
		   HttpSession session =  this.getHttpSession(request);
		   Map powerMap = null;
		   if(null != session){
			   powerMap = (Map)session.getAttribute("USERROLE_" + userId);
		   }
		   return powerMap;
	   }
	/**
	 * 获取当前登录用户的权限
	 * @param request
	 * @param roleCode
	 * @return
	 */
	protected String getAuthByRoleCode(HttpServletRequest request,String roleCode){
		 HttpSession session = this.getSession(request);
		 if(null != session){
			 HsAccount accObj = this.getCurrentuser(request);
			 Map authMap = (Map)session.getAttribute("USERPOWER_"+accObj.getId());
			 if(null != authMap){
				 return (String)authMap.get(roleCode);
			 }
		 }
		 return null;
	}
	/**
	 * 判断当前用户是否具有菜单权限
	 * @param request
	 * @param roleMap
	 * @param authId
	 * @param userId
	 * @return
	 */
	public boolean isHasAuth(HttpServletRequest request,Map roleMap,String authId,Long userId){
		//如果session中不存在该用户的权限信息则重新加载一次
		if( null == roleMap ){
			this.putUserPowerInsession(request, userId);
		} 
		if(1 == userId)
			return true;
		if(null != roleMap && null != roleMap.get(authId))
			return true;
 		return false;
	}
	protected DBMgr getDbo(HttpServletRequest request){
		   return  DBMgr.getDbUtilInst(request.getSession().getServletContext());
	}
	private static String USER_POWER_SQL = "SELECT to_char(PARENTID) as pid,to_char(ID)  as id FROM HS_POWER P WHERE P.ID IN(SELECT POWERID FROM HS_ROLEPOWER WHERE ROLEID IN(SELECT ROLEID FROM HS_ACCROLE WHERE ACCID=?)) AND P.LEVELID=3 AND P.ISUSE=0";

	/**
	    * 查询获取当前用户的菜单权限并放入session中
	    * @param request
	    */
	   public void putUserPowerInsession(HttpServletRequest request,Long userId){
		    Long uid = null;
		    if(null == userId){
			    HsAccount accinfo = this.getCurrentuser( request );
			    uid = accinfo.getId();
		    }else{
		    	uid = userId;
		    }
		    if(null == uid){
		    	System.out.println("没有获取当前用户");
		    	return;
		    }
		    Map roleMap = new HashMap(); 
	    	List powerList = this.getDbo(request).queryList(USER_POWER_SQL, new Object[]{uid});
	    	for(int i = 0;i<powerList.size();i++){
	    		Map power = (Map)powerList.get(i);
	    		String pid = (String)power.get("pid");
	    		String id = (String)power.get("id");
	    		String powername = (String)power.get("POWERNAME");
	    		if(roleMap.get(pid) == null){
	    			roleMap.put(pid, id);
	    		}
	    		roleMap.put(id, id);
	    	}
	    	Map roleMapList = new HashMap();
	    	List roleList = this.getDbo(request).queryList("select ROLECODE from HS_ROLE b where b.id in ( select ROLEID from HS_ACCROLE a where A.ACCID =?) group by ROLECODE",new Object[]{uid});
	    	for(int j = 0;j<roleList.size();j++){
	    		Map role = (Map)roleList.get(j);
	    		String roleCode = (String)role.get("ROLECODE");
	    		roleMapList.put(roleCode, roleCode);
	    	}
	    	this.getHttpSession(request).setAttribute("USERPOWER_" + uid,roleMap );
	    	this.getHttpSession(request).setAttribute("USERROLE_" + uid,roleMapList );
		    
	   }
	/**
	    * 获取当前所访问的路径
	    * @param request
	    * @return
	    */
   protected Map getCurrentMenuStr(HttpServletRequest request){
	   Map data = new HashMap();
	   String menuId = request.getParameter("menuid");
	   StringBuffer buffer = new StringBuffer("当前位置:");
	   if(null != menuId && StringUtils.isNotBlank(menuId) && !"null".equals(menuId)){
		   DictCacheUtil inst = DictCacheUtil.getInst();
		   Map menuObj = inst.getMenuMap(menuId);
		   if(menuObj != null){ 
			   String parentId = menuObj.get("PARENTID") + "";
			   Map menuParentObj = inst.getMenuMap(parentId);
			   buffer.append(menuParentObj==null?"":menuParentObj.get("POWERNAME"))
			   .append("->").append(menuObj==null?"":menuObj.get("POWERNAME"));
		   }
	   }
	   data.put("menuid", menuId);
	   data.put("menustr", buffer.toString());
 	   return data;
   }
 	
}
